import { Provide, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { TB_DPEngine_Info } from '../entity/EngineInfo.entity';
import { Repository } from 'typeorm';
import { DPEngineInfoCreateDto, DPEngineInfoUpdateDto, DPEngineInfoQueryDto, DPEngineInfoDeleteDto } from '../dto/EngineInfo.dto';

@Provide()
export class DPEngineInfoService {
  @InjectEntityModel(TB_DPEngine_Info)
  dpEngineInfoRepository: Repository<TB_DPEngine_Info>;

  // 添加引擎信息
  async addEngineInfo(params: DPEngineInfoCreateDto) {
    try {
      const engineInfo = new TB_DPEngine_Info();
      engineInfo.EngName = params.EngName;
      engineInfo.EngineType = params.EngineType;
      engineInfo.NetWork = params.NetWork;
      engineInfo.TrainName = params.TrainName;
      engineInfo.TrainPath = params.TrainPath;
      engineInfo.DetectName = params.DetectName;
      engineInfo.DetectPath = params.DetectPath;
      engineInfo.Mark = params.Mark;
      engineInfo.TrainPara = params.TrainPara;
      engineInfo.DetectPara = params.DetectPara;

      const result = await this.dpEngineInfoRepository.save(engineInfo);
      return {
        success: true,
        data: result,
        message: '新增引擎信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '新增引擎信息失败: ' + error.message,
      };
    }
  }

  // 获取所有引擎信息
  async getAllEngineInfo() {
    try {
      const engineInfos = await this.dpEngineInfoRepository.find({
        order: {
          CREATE_TIME: 'DESC',
        },
      });
      return {
        success: true,
        data: engineInfos,
        message: '获取所有引擎信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '获取所有引擎信息失败: ' + error.message,
      };
    }
  }

  // 根据查询条件获取引擎信息，支持分页
  async findEngineInfo(query: DPEngineInfoQueryDto) {
    const { EngName, EngineType, page = 1, limit = 10 } = query;
    const where: any = {};

    // 根据引擎名称查询
    if (EngName) {
      where['EngName'] = EngName;
    }

    // 根据引擎类型查询
    if (EngineType) {
      where['EngineType'] = EngineType;
    }

    try {
      const [engineInfos, total] = await this.dpEngineInfoRepository.findAndCount({
        where,
        skip: (page - 1) * limit, // 跳过的条数
        take: limit,              // 每页返回的条数
      });

      return {
        success: true,
        data: engineInfos,
        total,                      // 返回总条数
        message: '查询引擎信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '查询引擎信息失败: ' + error.message,
      };
    }
  }

  // 更新引擎信息
  async updateEngineInfo(params: DPEngineInfoUpdateDto) {
    const { OBJID } = params;
    try {
      const engineInfo = await this.dpEngineInfoRepository.findOne({
        where: { OBJID },
      });
      if (!engineInfo) {
        return {
          success: false,
          message: '要更新的引擎信息不存在',
        };
      }

      engineInfo.EngName = params.EngName ?? engineInfo.EngName;
      engineInfo.EngineType = params.EngineType ?? engineInfo.EngineType;
      engineInfo.NetWork = params.NetWork ?? engineInfo.NetWork;
      engineInfo.TrainName = params.TrainName ?? engineInfo.TrainName;
      engineInfo.TrainPath = params.TrainPath ?? engineInfo.TrainPath;
      engineInfo.DetectName = params.DetectName ?? engineInfo.DetectName;
      engineInfo.DetectPath = params.DetectPath ?? engineInfo.DetectPath;
      engineInfo.Mark = params.Mark ?? engineInfo.Mark;
      engineInfo.TrainPara = params.TrainPara ?? engineInfo.TrainPara;
      engineInfo.DetectPara = params.DetectPara ?? engineInfo.DetectPara;

      const result = await this.dpEngineInfoRepository.save(engineInfo);
      return {
        success: true,
        data: result,
        message: '更新引擎信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '更新引擎信息失败: ' + error.message,
      };
    }
  }

  // 删除引擎信息
  async deleteEngineInfo(params: DPEngineInfoDeleteDto) {
    const { OBJID } = params;
    try {
      const engineInfo = await this.dpEngineInfoRepository.findOne({
        where: { OBJID },
      });
      if (!engineInfo) {
        return {
          success: false,
          message: '要删除的引擎信息不存在',
        };
      }

      await this.dpEngineInfoRepository.remove(engineInfo);
      return {
        success: true,
        message: '删除引擎信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '删除引擎信息失败: ' + error.message,
      };
    }
  }
}
